package com.huangdada.hfweather.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 时间工具类
 * 
 * @ClassName: DateUtil
 * @author bob
 * @date 2016年2月16日 下午2:08:37
 */
public class DateUtil {
	public static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

	/**
	 * 格式：yyyy-MM-dd HH:mm:ss
	 */
	public static final String FMT_TYPE1 = "yyyy-MM-dd HH:mm:ss";
	/**
	 * 格式：yyyy-MM-dd
	 */
	public static final String FMT_TYPE2 = "yyyy-MM-dd";
	/**
	 * 格式：HH:mm:ss
	 */
	public static final String FMT_TYPE3 = "HH:mm:ss";
	/**
	 * 格式：yyyyMMddHHmmss
	 */
	public static final String FMT_TYPE4 = "yyyyMMddHHmmss";
	/**
	 * 格式：yyyyMMdd
	 */
	public static final String FMT_TYPE5 = "yyyyMMdd";
	/**
	 * 格式：HHmmss
	 */
	public static final String FMT_TYPE6 = "HHmmss";
	/**
	 * 格式：HHmmssSSS
	 */
	public static final String FMT_TYPE7 = "HHmmssSSS";
	/**
	 * 格式：yyyyMMddHHmmssSSS
	 */
	public static final String FMT_TYPE8 = "yyyyMMddHHmmssSSS";

	/**
	 * 变量：日期格式化类型 - 格式:yyyy/MM/dd
	 */
	public static final int DEFAULT = 0;
	public static final int YM = 1;

	/**
	 * 变量：日期格式化类型 - 格式:yyyy-MM-dd
	 * 
	 */
	public static final int YMR_SLASH = 11;

	/**
	 * 变量：日期格式化类型 - 格式:yyyyMMdd
	 * 
	 */
	public static final int NO_SLASH = 2;

	/**
	 * 变量：日期格式化类型 - 格式:yyyyMM
	 * 
	 */
	public static final int YM_NO_SLASH = 3;

	/**
	 * 变量：日期格式化类型 - 格式:yyyy/MM/dd HH:mm:ss
	 * 
	 */
	public static final int DATE_TIME = 4;

	/**
	 * 变量：日期格式化类型 - 格式:yyyyMMddHHmmss
	 * 
	 */
	public static final int DATE_TIME_NO_SLASH = 5;

	/**
	 * 变量：日期格式化类型 - 格式:yyyy/MM/dd HH:mm
	 * 
	 */
	public static final int DATE_HM = 6;

	/**
	 * 变量：日期格式化类型 - 格式:HH:mm:ss
	 * 
	 */
	public static final int TIME = 7;

	/**
	 * 变量：日期格式化类型 - 格式:HH:mm
	 * 
	 */
	public static final int HM = 8;

	/**
	 * 变量：日期格式化类型 - 格式:HHmmss
	 * 
	 */
	public static final int LONG_TIME = 9;
	/**
	 * 变量：日期格式化类型 - 格式:HHmm
	 * 
	 */

	public static final int SHORT_TIME = 10;

	/**
	 * 变量：日期格式化类型 - 格式:yyyy-MM-dd HH:mm:ss
	 */
	public static final int DATE_TIME_LINE = 12;

	/**
	 * 变量：日期格式化类型 - 格式:yyyyMMddHHmmssSSS
	 * 
	 */
	public static final int DATE_TIME_SLASH = 13;

	public static String dateToStr(Date date, int type) {
		switch (type) {
		case DEFAULT:
			return dateToStr(date);
		case YM:
			return dateToStr(date, "yyyy/MM");
		case NO_SLASH:
			return dateToStr(date, "yyyyMMdd");
		case YMR_SLASH:
			return dateToStr(date, "yyyy-MM-dd");
		case YM_NO_SLASH:
			return dateToStr(date, "yyyyMM");
		case DATE_TIME:
			return dateToStr(date, "yyyy/MM/dd HH:mm:ss");
		case DATE_TIME_NO_SLASH:
			return dateToStr(date, "yyyyMMddHHmmss");
		case DATE_HM:
			return dateToStr(date, "yyyy/MM/dd HH:mm");
		case TIME:
			return dateToStr(date, "HH:mm:ss");
		case HM:
			return dateToStr(date, "HH:mm");
		case LONG_TIME:
			return dateToStr(date, "HHmmss");
		case SHORT_TIME:
			return dateToStr(date, "HHmm");
		case DATE_TIME_LINE:
			return dateToStr(date, "yyyy-MM-dd HH:mm:ss");
		case DATE_TIME_SLASH:
			return dateToStr(date, "yyyyMMddHHmmssSSS");
		default:
			throw new IllegalArgumentException("Type undefined : " + type);
		}
	}

	public static String dateToStr(Date date, String pattern) {
		if (date == null || date.equals(""))
			return null;
		SimpleDateFormat formatter = new SimpleDateFormat(pattern);
		return formatter.format(date);
	}

	public static String dateToStr(Date date) {
		return dateToStr(date, "yyyy/MM/dd");
	}

	/**
	 * date转成字符串
	 */
	public static String dateToString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 字符串类型转成Date类型
	 */
	public static Date stringToDate(String dateStr) {
		return stringToDate(dateStr, "yyyy-MM-dd");
	}

	public static Date stringToDate(String dateStr, int type) {
		switch (type) {
		case DEFAULT:
			return stringToDate(dateStr);
		case YM:
			return stringToDate(dateStr, "yyyy/MM");
		case NO_SLASH:
			return stringToDate(dateStr, "yyyyMMdd");
		case YMR_SLASH:
			return stringToDate(dateStr, "yyyy-MM-dd");
		case YM_NO_SLASH:
			return stringToDate(dateStr, "yyyyMM");
		case DATE_TIME:
			return stringToDate(dateStr, "yyyy/MM/dd HH:mm:ss");
		case DATE_TIME_NO_SLASH:
			return stringToDate(dateStr, "yyyyMMddHHmmss");
		case DATE_HM:
			return stringToDate(dateStr, "yyyy/MM/dd HH:mm");
		case TIME:
			return stringToDate(dateStr, "HH:mm:ss");
		case HM:
			return stringToDate(dateStr, "HH:mm");
		case LONG_TIME:
			return stringToDate(dateStr, "HHmmss");
		case SHORT_TIME:
			return stringToDate(dateStr, "HHmm");
		case DATE_TIME_LINE:
			return stringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
		case DATE_TIME_SLASH:
			return stringToDate(dateStr, "yyyyMMddHHmmssSSS");
		default:
			throw new IllegalArgumentException("Type undefined : " + type);
		}
	}

	/**
	 * 字符串类型转成Date类型
	 */
	public static Date stringToDate(String dateStr, String pattern) {
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			date = sdf.parse(dateStr);
		} catch (Exception e) {
			logger.error("date parse error", e);
		}
		return date;
	}

	/**
	 * 获取Date当前月最第一天
	 */
	public static Date getMouthFirstDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);
		return calendar.getTime();
	}

	/**
	 * 获取Date当前月最后一天
	 */
	public static Date getMouthLastDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);
		calendar.roll(Calendar.DATE, -1);
		return calendar.getTime();
	}
	/**
	 * 获取Date当前月最第一天
	 */
	public static Date getMouthFirstDateHHmmss(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取Date当前月最后一天
	 */
	public static Date getMouthLastDateHHmmss(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, 1);
		calendar.roll(Calendar.DATE, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}
	/**
	 * 获取Date当前月-所有日期
	 */
	public static String getMouthDates(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int MaxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		StringBuffer sb = new StringBuffer("[");
		for (int i = 1; i <= MaxDay; i++) {
			if (i < 10) {
				sb.append("'" + "0" + i + "',");
			} else {
				sb.append("'" + i + "',");
			}
		}
		String str = sb.substring(0, sb.length() - 1);
		return str + "]";
	}

	/**
	 * 获取Date当前月-所有日期
	 */
	public static List<String> getMouthDatesList(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int MaxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		List<String> list = new ArrayList<>();
		for (int i = 1; i <= MaxDay; i++) {
			if (i < 10) {
				list.add("0" + i);
			} else {
				list.add("" + i);
			}
		}
		return list;
	}

	/**
	 * 获取Date当前年最第一天
	 */
	public static Date getYearFirstDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int currentYear = calendar.get(Calendar.YEAR);
		calendar.clear();
		calendar.set(Calendar.YEAR, currentYear);
		return calendar.getTime();
	}

	/**
	 * 获取Date当前年最后一天
	 */
	public static Date getYearLastDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int currentYear = calendar.get(Calendar.YEAR);
		calendar.clear();
		calendar.set(Calendar.YEAR, currentYear);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		return calendar.getTime();
	}

	/**
	 * 获取Date当前年-所有月份
	 */
	public static String getYearMouths(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		StringBuffer sb = new StringBuffer("[");
		for (int i = 1; i <= 12; i++) {
			if (i < 10) {
				sb.append("'" + year + "0" + i + "',");
			} else {
				sb.append("'" + year + i + "',");
			}
		}
		String str = sb.substring(0, sb.length() - 1);
		return str + "]";
	}

	/**
	 * 获取Date当前年-所有月份
	 */
	public static List<String> getYearMouthsList(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		List<String> list = new ArrayList<>();
		for (int i = 1; i <= 12; i++) {
			if (i < 10) {
				list.add(year + "0" + i);
			} else {
				list.add(year + "" + i);
			}
		}
		return list;
	}

	/**
	 * 获取两个时间相差的秒数
	 */
	public static int getIntervalSecond(Date startDate, Date endDate) {
		long end = endDate.getTime();
		long start = startDate.getTime();
		int c = (int) ((end - start) / 1000);
		return c;
	}

	/**
	 * 去掉date的时分秒毫秒
	 */
	public static Date getDateRemoveHourMinuteSecond(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 将时分秒,毫秒域清零
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	// 获得某天最小时间 2017-10-15 00:00:00.000
	public static Date getStartOfDay(Date date) {
		return getDateRemoveHourMinuteSecond(date);
	}

	// 获得某天最大时间 2017-10-15 23:59:59.999
	public static Date getEndOfDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 将时分秒,毫秒域 设置为 23:59:59.999
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}

	/**
	 * 获得指定日期的前一天
	 * 
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static Date getSpecifiedDayBefore(Date nowDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);
		return c.getTime();
	}
	/**
	 * 获得指定日期的前一天 开始时间
	 * @param nowDate
	 * @return
	 */
	public static Date getSpecifiedDayBeforeStart(Date nowDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);
		// 将时分秒,毫秒域清零
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
	/**
	 * 获得指定日期的前一天 结束时间
	 * @param nowDate
	 * @return
	 */
	public static Date getSpecifiedDayBeforeEnd(Date nowDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);
		// 将时分秒,毫秒域清零
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c.getTime();
	}

	/**
	 * 获得指定日期的前n天 开始时间
	 * @param nowDate
	 * @return
	 */
	public static Date getSpecifiedDayBeforeStart(Date nowDate,int n) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - n);
		// 将时分秒,毫秒域清零
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		return c.getTime();
	}
	/**
	 * 获得指定日期的前n天 结束时间
	 * @param nowDate
	 * @return
	 */
	public static Date getSpecifiedDayBeforeEnd(Date nowDate,int n) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - n);
		// 将时分秒,毫秒域清零
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c.getTime();
	}
	/**
	 * 获得指定日期的后一天
	 * 
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static Date getSpecifiedDayAfter(Date nowDate) {
		Calendar c = Calendar.getInstance();
		c.setTime(nowDate);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);
		return c.getTime();
	}

	/**
	 * 获取上一个月 最后一天
	 * 
	 * @return
	 */
	public static Date getLastMonthLastDay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return stringToDate(format.format(cale.getTime()) + " 23:59:59", DATE_TIME_LINE);
	}

	/**
	 * 获取上一个月 第一天
	 * 
	 * @return
	 */
	public static Date getLastMonthFirstDay() {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal_1 = Calendar.getInstance();// 获取当前日期
		cal_1.add(Calendar.MONTH, -1);
		cal_1.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		format.format(cal_1.getTime());
		
		return stringToDate(format.format(cal_1.getTime()) + " 00:00:00", DATE_TIME_LINE);
	}
	
	/**
	 * 判断日期相等(去掉时分秒)
	 * 
	 * @return
	 */
	public static boolean isEqualsDateIngoreHourMinuteSecond(Date date1, Date date2) {
		return DateUtil.getDateRemoveHourMinuteSecond(date1).equals(DateUtil.getDateRemoveHourMinuteSecond(date2));
	}

	public static void main(String[] args) {

		/*
		 * long time = 1467706802; try { System.out.println(new
		 * Date(time).toLocaleString()); } catch (Exception e) {
		 * e.printStackTrace(); }
		 * 
		 * System.out.println(dateToStr(new Date(), FMT_TYPE8));
		 * 
		 * Calendar calendar = Calendar.getInstance();
		 * 
		 * System.out.println(dateToStr(calendar.getTime(), FMT_TYPE8));
		 * 
		 * calendar.set(Calendar.MILLISECOND, 0);
		 * 
		 * System.out.println(dateToStr(calendar.getTime(), FMT_TYPE8));
		 * 
		 * System.out.println(getDateRemoveHourMinuteSecond(new
		 * Date()).toLocaleString());
		 */
		
		// 获取前月的第一天
		System.out.println(getSpecifiedDayBeforeStart(new Date(),7));
		
		System.out.println(DateUtil.isEqualsDateIngoreHourMinuteSecond(DateUtil.stringToDate("2019-03-14", YMR_SLASH), DateUtil.stringToDate("2019-03-20 17:38:12", DATE_TIME_LINE)));
	}
}
